/**
 * Created by Administrator on 14-4-26.
 */

$(document).ready(function(){
    $(".accordion-body").removeClass('in');

    $(".left-menu-nav li a").each(function() {
        var href = window.location.href;
        if(href.indexOf($(this).attr('href'))>=0){
            $(this).addClass('active');
            $(this).parent().parent().parent().parent().addClass('in');
            $(this).parent().parent().show();
        }
    });

    //输入框自动计算输入值的长度，并做限制
    $(".length-tip").each(function() {
        var inputName = $(this).attr("for");
        var lengthLimit = parseInt($(this).attr("data-limit"));
        console.log(inputName, $('[name='+inputName+']').val());
        var currentLength = $('[name='+inputName+']').val().length;

        $(this).html(lengthLimitText(lengthLimit, currentLength));

        setTimeout(function(){
            var editor = eval('CKEDITOR.instances.'+inputName);
                if(editor != undefined){
                    editor.on("instanceReady", function () {
                        var lastData = this.getData();
                        var inputName = this.name;

                        var lengthTip = $(".length-tip[for="+inputName+"]");

                        var data = editor.getData().replace(/<[^>]+>/g,"");
                        var currentLength = data.length>0?(data.length-1):data.length;

                        if(currentLength > lengthLimit){
                            lengthTip.addClass('length-limit-max');
                            lengthTip.html(lengthLimitText(lengthLimit, lengthLimit));
                        }else{
                            lengthTip.removeClass('length-limit-max');
                            lengthTip.html(lengthLimitText(lengthLimit, currentLength));
                        }
                    });


                    //set keyup event
                    editor.document.on("keyup", function(){

                        var lengthTip = $(".length-tip[for="+inputName+"]");
                        var lengthLimit = parseInt(lengthTip.attr("data-limit"));

                        var data = editor.getData().replace(/<[^>]+>/g,"");
                        var currentLength = data.length>0?(data.length-1):data.length;

                        if(currentLength > lengthLimit){
                            this.getBody().setText(lastData);
                            lengthTip.addClass('length-limit-max');
                            lengthTip.removeClass('length-limit-max');
                            lengthTip.html(lengthLimitText(lengthLimit, lengthLimit));
                        }else{
                            lastData = this.getBody().getText();
                            lengthTip.removeClass('length-limit-max');
                            lengthTip.html(lengthLimitText(lengthLimit, currentLength));
                        }
                    });
                }
        }, 500);

        $('[name='+inputName+']').keyup(function(){
            var self = $(this);
            var inputName = self.attr("name");
            var lengthTip = $(".length-tip[for="+inputName+"]");
            currentLength = self.val().length;

            if(currentLength > lengthLimit){
                var value = self.val().substr(0, lengthLimit);
                lengthTip.addClass('length-limit-max');
                lengthTip.removeClass('length-limit-max');
                self.val(value);
                lengthTip.html(lengthLimitText(lengthLimit, lengthLimit));
            }else{
                lengthTip.removeClass('length-limit-max');
                lengthTip.html(lengthLimitText(lengthLimit, currentLength));
            }
        });

    });

    function lengthLimitText(lengthLimit, currentLength){
        return "字数最多："+lengthLimit+" 剩余字数："+(lengthLimit-currentLength);
    }

    //禁用Enter键表单自动提交
    document.onkeydown = function(event) {
        var target, code, tag;
        if (!event) {
            event = window.event; //针对ie浏览器
            target = event.srcElement;
            code = event.keyCode;
            if (code == 13) {
                tag = target.tagName;
                if (tag == "TEXTAREA") { return true; }
                else { return false; }
             }
         }
         else {
            target = event.target; //针对遵循w3c标准的浏览器，如Firefox
            code = event.keyCode;
            if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") { return false; }
            else { return true; }
            }
         }
    };
});


